package com.jsmtr.rms.service.impl;

import java.util.List;
import java.util.Map;

import com.jsmtr.rms.entity.HRSchoolUsers;
import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.service.ISchoolService;
import com.jsmtr.rms.utils.db.DbUtil;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service
public class SchoolService implements ISchoolService {

    @Resource
    private DaoService dao;

    public void setDao(DaoService dao) {
        this.dao = dao;
    }

    @Override
    public Map<String, Object> getSchools(int page, int perpage, int state, String key) throws Exception {
        StringBuffer queryHQL = new StringBuffer("from HRSchoolUsers ");
        StringBuffer countHQL = new StringBuffer("select count(*) from HRSchoolUsers ");
        StringBuffer condition = new StringBuffer("where (ucode like '%" + key + "%' or uname like '%" + key + "%') ");
        if (state != -1)
            condition.append("and ustate='" + state + "'");

        queryHQL.append(condition);
        countHQL.append(condition);

        return DbUtil.pageQuery(page, perpage, queryHQL.toString(), countHQL.toString(), dao);
    }


    @Override
    public HRSchoolUsers getSchool(String id) throws Exception {
        return (HRSchoolUsers) dao.findUnique("from HRSchoolUsers where id='" + id + "'");
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean hasSchool(String ucode) throws Exception {
        try {
            List<HRSchoolUsers> users = dao.find("from HRSchoolUsers where ucode='" + ucode + "'");
            if (users.size() > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<HRSchoolUsers> getSchools() throws Exception {
        return dao.find("from HRSchoolUsers ");
    }

}